package io.webfolder.cdp.command;

import java.util.List;

import com.google.gson.reflect.TypeToken;

import io.webfolder.cdp.session.SessionInvocationHandler;
import io.webfolder.cdp.type.webauthn.Credential;
import io.webfolder.cdp.type.webauthn.VirtualAuthenticatorOptions;

public class WebAuthnImpl implements WebAuthn {

	private static final String[] EMPTY_ARGS = new String[] {};
	private static final Object[] EMPTY_VALUES = new Object[] {};
	private static final TypeToken<List<Credential>> GET_CREDENTIALS = new TypeToken<List<Credential>>() {
	};
	private final SessionInvocationHandler handler;

	public WebAuthnImpl(SessionInvocationHandler handler) {
		this.handler = handler;
	}

	@Override
	public void addCredential(String authenticatorId, Credential credential) {
		handler.invoke("WebAuthn", "addCredential", "WebAuthn.addCredential", null, void.class, null, true, false,
				false, new String[] { "authenticatorId", "credential" }, new Object[] { authenticatorId, credential });
	}

	@Override
	public String addVirtualAuthenticator(VirtualAuthenticatorOptions options) {
		return (String) handler.invoke("WebAuthn", "addVirtualAuthenticator", "WebAuthn.addVirtualAuthenticator",
				"authenticatorId", String.class, null, false, false, false, new String[] { "options" },
				new Object[] { options });
	}

	@Override
	public void clearCredentials(String authenticatorId) {
		handler.invoke("WebAuthn", "clearCredentials", "WebAuthn.clearCredentials", null, void.class, null, true, false,
				false, new String[] { "authenticatorId" }, new Object[] { authenticatorId });
	}

	@Override
	public void disable() {
		handler.invoke("WebAuthn", "disable", "WebAuthn.disable", null, void.class, null, true, false, true, EMPTY_ARGS,
				EMPTY_VALUES);
	}

	@Override
	public void enable() {
		handler.invoke("WebAuthn", "enable", "WebAuthn.enable", null, void.class, null, true, true, false, EMPTY_ARGS,
				EMPTY_VALUES);
	}

	@Override
	@java.lang.SuppressWarnings("unchecked")
	public List<Credential> getCredentials(String authenticatorId) {
		return (List<Credential>) handler.invoke("WebAuthn", "getCredentials", "WebAuthn.getCredentials", "credentials",
				List.class, GET_CREDENTIALS.getType(), false, false, false, new String[] { "authenticatorId" },
				new Object[] { authenticatorId });
	}

	@Override
	public void removeVirtualAuthenticator(String authenticatorId) {
		handler.invoke("WebAuthn", "removeVirtualAuthenticator", "WebAuthn.removeVirtualAuthenticator", null,
				void.class, null, true, false, false, new String[] { "authenticatorId" },
				new Object[] { authenticatorId });
	}

	@Override
	public void setUserVerified(String authenticatorId, Boolean isUserVerified) {
		handler.invoke("WebAuthn", "setUserVerified", "WebAuthn.setUserVerified", null, void.class, null, true, false,
				false, new String[] { "authenticatorId", "isUserVerified" },
				new Object[] { authenticatorId, isUserVerified });
	}
}